﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Smartkernel.Net
{
    /// <summary>
    /// 智能邮件相关功能
    /// </summary>
    public static class SmartMail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailMessage">邮件内容</param>
        /// <param name="smtpClient">发送服务器</param>
        /// <param name="networkCredential">身份验证</param>
        /// <param name="timeout">超时时间</param>
        public static void Send(MailMessage mailMessage, SmtpClient smtpClient, NetworkCredential networkCredential, int timeout)
        {
            smtpClient.Credentials = networkCredential;
            //smtpClient.UseDefaultCredentials = true;
            smtpClient.Timeout = timeout * 1000;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Send(mailMessage);
        }
         /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">接收者，例如：test1@126.com,test2@126.com</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="htmlBody">邮件正文</param>
        /// <param name="smtp">发送服务器，例如：smtp.126.com</param>
        /// <param name="port">发送服务器端口</param>
        /// <param name="senderEmail">发送者邮箱，必须是发送服务器的邮箱，例如：caoyanbai@gmail.com</param>
        /// <param name="senderUserName">发送者，必须是发送者邮箱的用户名，例如：smartkernel</param>
        /// <param name="senderPassword">密码，必须是发送者邮箱的密码，例如：kenengme</param>
        /// <param name="timeout">超时时间</param>
        public static void Send(string to, string subject, string htmlBody, string smtp, int port, string senderEmail, string senderUserName, string senderPassword, int timeout)
        {
            Send(to, subject, htmlBody, smtp, port, senderEmail, senderUserName, senderPassword, timeout, Encoding.GetEncoding("gb2312"));
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">接收者，例如：test1@126.com,test2@126.com</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="htmlBody">邮件正文</param>
        /// <param name="smtp">发送服务器，例如：smtp.126.com</param>
        /// <param name="port">发送服务器端口</param>
        /// <param name="senderEmail">发送者邮箱，必须是发送服务器的邮箱，例如：caoyanbai@gmail.com</param>
        /// <param name="senderUserName">发送者，必须是发送者邮箱的用户名，例如：smartkernel</param>
        /// <param name="senderPassword">密码，必须是发送者邮箱的密码，例如：kenengme</param>
        /// <param name="timeout">超时时间</param>
        /// <param name="encoding">编码方式</param>
        public static void Send(string to, string subject, string htmlBody, string smtp, int port, string senderEmail, string senderUserName, string senderPassword, int timeout, Encoding encoding)
        {
            using (MailMessage mailMessage = new MailMessage())
            {
                mailMessage.To.Add(to);
                mailMessage.Subject = subject;
                mailMessage.Body = htmlBody;
                mailMessage.IsBodyHtml = true;
                mailMessage.From = new MailAddress(senderEmail);
                mailMessage.Priority = MailPriority.High;
                mailMessage.BodyEncoding = encoding;

                SmtpClient smtpClient = new SmtpClient(smtp, port);

                NetworkCredential networkCredential = new NetworkCredential(senderUserName, senderPassword);

                Send(mailMessage, smtpClient, networkCredential, timeout);
            }
        }
        /// <summary>
        /// 发送邮件，配置文件SmartMailSenderEntity.config必须存在
        /// </summary>
        /// <param name="to">接收者，例如：test1@126.com,test2@126.com</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="htmlBody">邮件正文</param>
        public static void Send(string to, string subject, string htmlBody)
        {
            Send(to, subject, htmlBody, SmartMailSenderEntity.Instance);
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">接收者，例如：test1@126.com,test2@126.com</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="htmlBody">邮件正文</param>
        /// <param name="entity">配置实体</param>
        public static void Send(string to, string subject, string htmlBody, SmartMailSenderEntity entity)
        {
            Send(to, subject, htmlBody, entity.Smtp, entity.Port, entity.SenderEmail, entity.SenderUserName, entity.SenderPassword, entity.Timeout);
        }
    }
}
